package com.fitbet.admin.web.controller.bet;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.fitbet.admin.dto.BetRequest;
import com.fitbet.admin.service.bet.BetService;
import com.fitbet.entity.bet.Bet;
import com.fitbet.entity.bet.UserBet;

/**
 * 赌局管理
 * 
 * @author sjh
 *
 */
@Controller
@RequestMapping("/bets")
@MenuSetting("menu-bet")
public class BetController {

    private static final Logger logger = LoggerFactory.getLogger(BetController.class);

    @Autowired
    private BetService betService;


    /**
     * 
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-bet-list")
    public ModelAndView list(Page page, HttpServletRequest request) {

        ModelAndView modelAndView = new ModelAndView("bet/bet/bet-list");

        int totals = betService.count();

        if (totals > 0) {

            List<Bet> bets = betService.list(page.with(totals));

            modelAndView.addObject("bets", bets);
            modelAndView.addObject("page", page);

        }
        modelAndView.addObject("queryString",
                WebUtils.extractQueryString(request, "page", "filter"));

        return modelAndView;
    }

    /**
     * 
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    @MenuSetting("submenu-bet-list")
    public ModelAndView create() {

        ModelAndView modelAndView = new ModelAndView("bet/bet/bet-create");

        return modelAndView;
    }

    /**
     * 
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public HttpResult onCreate(BetRequest betRequest) {

        HttpResult httpResult = new HttpResult();

        try {

            betService.create(betRequest);
            httpResult.success().message("添加成功");

        } catch (Exception e) {

            logger.error("onCreate occurs error", e);
            httpResult.fail("500").message("添加失败");
        }


        return httpResult;
    }


    /**
     * 开启赌局
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/open", method = RequestMethod.GET)
    @MenuSetting("submenu-bet-list")
    public ModelAndView open(@PathVariable("id") Long id) {

        ModelAndView modelAndView = new ModelAndView("bet/bet/bet-open");

        return modelAndView;
    }

    /**
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/open", method = RequestMethod.PATCH,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public HttpResult onOpen(@PathVariable("id") Long id) {

        HttpResult httpResult = new HttpResult();

        try {

            betService.open(id);

            httpResult.success().message("开启成功");

        } catch (Exception e) {

            logger.error("onOpen occurs error", e);

            httpResult.fail("500").message("开启失败");

        }

        return httpResult;
    }


    /**
     * 关闭赌局
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/close", method = RequestMethod.GET)
    @MenuSetting("submenu-bet-list")
    public ModelAndView close(@PathVariable("id") Long id) {

        ModelAndView modelAndView = new ModelAndView("bet/bet/bet-close");

        return modelAndView;
    }


    /**
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/close", method = RequestMethod.PATCH,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public HttpResult onClose(@PathVariable("id") Long id) {

        HttpResult httpResult = new HttpResult();

        try {

            betService.close(id);

            httpResult.success().message("关闭成功");

        } catch (Exception e) {

            logger.error("onClose occurs error", e);

            httpResult.fail("500").message("关闭失败");
        }

        return httpResult;
    }

    /**
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @MenuSetting("submenu-bet-list")
    public ModelAndView view(@PathVariable("id") Long id) {

        ModelAndView modelAndView = new ModelAndView("bet/bet/bet-view");

        Bet bet = betService.load(Bet.class, id);

        List<UserBet> userBets = betService.listById(id);

        modelAndView.addObject("userBets", userBets);

        modelAndView.addObject("bet", bet);

        return modelAndView;

    }

    /**
     * 关闭赌局
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/edit-cover", method = RequestMethod.GET)
    @MenuSetting("submenu-bet-list")
    public ModelAndView coverEdit(@PathVariable("id") Long id) {

        ModelAndView modelAndView = new ModelAndView("bet/bet/bet-cover-edit");
        modelAndView.addObject("betId", id);
        return modelAndView;
    }


    /**
     * 
     * @param id
     * @param media
     * @return
     */
    @RequestMapping(value = "/{id}/edit-cover", method = RequestMethod.PATCH,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public HttpResult onEditCover(@PathVariable("id") Long id,
            @RequestParam("mediaId") String media) {

        HttpResult httpResult = new HttpResult();

        try {

            betService.editCover(id, media);

            httpResult.success().message("编辑成功");

        } catch (Exception e) {

            logger.error("onClose occurs error", e);

            httpResult.fail("500").message("编辑失败");
        }

        return httpResult;
    }
}
